<?php
namespace jy_weishop;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
	public function statuscheck($order=array(),$status=0){
		$shopset=$this->plugin_setting('shopset');
		$shopset['orderpay_outtime']=intval($shopset['orderpay_outtime'])?:24;
		$orderpay_outtime=TIMESTAMP-intval($shopset['orderpay_outtime'])*3600;
		if($status==0 && $order['createtime'] < $orderpay_outtime){
			exi('订单已超时关闭，不能修改为【待付款状态】状态。移步：后台设置--基础设置--订单管理可以设置超时时间！','error');
		}
		$shopset['ordercheck_outtime']=intval($shopset['ordercheck_outtime'])?:7;
		$ordercheck_outtime=TIMESTAMP-intval($shopset['ordercheck_outtime'])*24*3600;
		if($status==2 && $order['createtime'] < $ordercheck_outtime){
			exi('订单已超时确认收货，不能修改为【待收货状态】状态。移步：后台设置--基础设置--订单管理可以设置超时时间！','error');
		}
		if($status==1 && intval($shopset['orderfinish_outtime'])>0){
			$orderfinish_outtime=TIMESTAMP-intval($shopset['orderfinish_outtime'])*3600;
			if($status==1 && $order['createtime'] < $orderfinish_outtime){
				exi('订单已超时自动发货，不能修改为【待发货状态】状态。移步：后台设置--基础设置--订单管理可以设置超时时间！','error');
			}
		}
		return true;
	}
	/*
		订单状态UP
	*/
	public function orderup($ismanage=false){
		$shopset=$this->plugin_setting('shopset');
		$params=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0);
		$pindex = max(1, 0);
		$psize = 10;
		$total='';
		if(!$ismanage){
			$params['mid']=$_SESSION['uid'];
		}
		//关闭订单
		$params['status']=0;
		$shopset['orderpay_outtime']=intval($shopset['orderpay_outtime'])?:24;
		$orderpay_outtime=TIMESTAMP-intval($shopset['orderpay_outtime'])*3600;
		$params['createtime <']=$orderpay_outtime;
		$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
		if($order){
			foreach($order as $a){
				if($a['id']){
					$this->model('order')->order_cancel($a['id'],true);
				}			
			}
		}
		//确认收货
		$params['status']=2;
		$shopset['ordercheck_outtime']=intval($shopset['ordercheck_outtime'])?:7;
		$ordercheck_outtime=TIMESTAMP-intval($shopset['ordercheck_outtime'])*24*3600;
		unset($params['createtime <']);
		$params['sendtime between']=array(10000,$ordercheck_outtime);
		$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
		if($order){
			foreach($order as $a){
				$getgoods=pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$a['id']),array('goodsid','total','optionid'));
				//核销商品,不确认完成
				$isverify=false;
				foreach($getgoods as $item){
					$g=$this->get('goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']),array('type','isverify'));
					if($g['isverify']==1){
						$isverify=true;
					}
				}
				if(!$isverify){
					$this->model('order')->order_receive($a['id'],true);
				}
			}
		}
		//已支付，未发货订单，超时完成
		if(intval($shopset['orderfinish_outtime'])>0){
			$params['status']=1;
			$orderfinish_outtime=TIMESTAMP-intval($shopset['orderfinish_outtime'])*3600;
			$params['createtime <']=$orderfinish_outtime;
			$order = pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,array('id'));
			if($order){
				foreach($order as $a){
					$this->model('order')->order_receive($a['id'],true);
				}
			}
		}
		//删除活动订单
		$this->model('activity')->order_task();
	}
	/*
		待领取优惠券弹窗任务
	*/
	public function pop_coupon_wait(){
		if($_SESSION['uid']){
			$giveall=$this->pdo_getall('coupon_wait',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'nolook'=>1));
			$coupon_wait=array();
			if($giveall){
				$alltotal=0;
				foreach($giveall as $k=>$give){
					$ac_types=array('huodong'=>'活动送券','goods'=>'购物送券','share'=>'分享送券','new'=>'新人发券','enough'=>'满额送券','order'=>'满额送券','use'=>'用券送券','pingjia'=>'评价送券');
					$give['typetitle']=$ac_types[$give['ac_type']];
					if($give['ac_type']=='huodong'){
						$sendtasktable='jy_weishop_coupon_huodongsendtask';
					}elseif($give['ac_type']=='goods'){
						$sendtasktable='jy_weishop_coupon_goodsendtask';
					}elseif($give['ac_type']=='share'){
						$sendtasktable='jy_weishop_coupon_sendticket_share';
					}elseif($give['ac_type']=='new'){
						$sendtasktable='jy_weishop_coupon_sendticket';
					}elseif($give['ac_type']=='enough' || $give['ac_type']=='order' || $give['ac_type']=='pingjia'){
						$sendtasktable='jy_weishop_coupon_sendtasks';
					}elseif($give['ac_type']=='use'){
						$sendtasktable='jy_weishop_coupon_usesendtasks';
					}
					if(!$sendtasktable){
						continue;
					}
					$couponids=pdo_getcolumn($sendtasktable,array('uniacid'=>$_SESSION['uniacid'],'id'=>$give['ac_id']),'couponid');
					if(!is_numeric($couponids)){
						$couponids=str_array($couponids);
						foreach($couponids as $cv){
							$couponid=pdo_getcolumn('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cv),'id');
							$canget=$this->model('coupon')->checkcanget($couponid);
							if($canget){
								break;
							}
						}
						$total=count($couponids);
					}else{
						$couponid=$couponids;
						$canget=$this->model('coupon')->checkcanget($couponid);
						$total=1;
					}
					$alltotal +=$total;
					$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$couponid),array('id','backtype','discount','deduct','sendtitle','sendimage','senddesc'));
					if(!$coupon['id'] || !$canget){
						unset($giveall[$k]);
						continue;
					}
					$nickname=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'nickname');
					$give['sendtitle']=str_replace('[nickname]',$nickname,$coupon['sendtitle']);
					if(!$sendtitle){
						$sendtitle=$give['sendtitle'];
					}
					$give['sendtitle']=str_replace('[total]',$total,$give['sendtitle']);
					$give['sendimage']=$coupon['sendimage'];
					$give['senddesc']=$coupon['senddesc'];
					if($coupon['backtype']==0){
							$give['fee']=intval($coupon['deduct']);
					}elseif($coupon['backtype']==1){
							$give['fee']=$coupon['discount'].'折';
					}elseif($coupon['backtype']==2){
							$give['fee']='立返';
					}
					$coupon_wait[]=$give;
				}
				$coupon_wait[0]['sendtitle']=str_replace('[total]',$alltotal,$sendtitle);
				$this->pdo_update('coupon_wait',array('nolook'=>2),array('uniacid'=>$_SESSION['uniacid'],'nolook'=>1,'mid'=>$_SESSION['uid']));
				return $coupon_wait;
			}
		}
		return false;
	}
	/*
		优惠券弹窗任务
	*/
	public function pop_coupon_hd(){
		if($_SESSION['uid']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
			$huodongs=pdo_getall('jy_weishop_coupon_huodongsendtask',$params);
			foreach($huodongs as $k=>&$item){
				$item['limittime']=json_decode($item['limittime'],true);
				$isgive=$this->pdo_get('coupon_wait',array('ac_type'=>'huodong','ac_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'id');
				if(empty($isgive['id']) && $item['limittime']['tstart']<time() && $item['limittime']['tend']>time()){
					$this->model('coupon')->getcoupon($item['couponid'],'','',true);
					$coupon=pdo_get('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['couponid']),array('backtype','discount','deduct','sendtitle','sendimage','senddesc'));
					$item['sendtitle']=$coupon['sendtitle'];
					$item['sendimage']=$coupon['sendimage'];
					$item['senddesc']=$coupon['senddesc'];
					if($coupon['backtype']==0){
						$item['fee']=$coupon['deduct'];
					}elseif($coupon['backtype']==1){
						$item['fee']=$coupon['discount'].'折';
					}elseif($coupon['backtype']==2){
						$item['fee']='立返';
					}
					$give=array(
						'uniacid'=>$_SESSION['uniacid'],
						'mid'=>$_SESSION['uid'],
						'status'=>1,
						'createtime'=>TIMESTAMP,
						'remark'=>'活动送券',
						'ac_id'=>$item['id'],
						'ac_type'=>'huodong',
					);
					$this->pdo_insert('coupon_wait',$give);
					return $huodongs[$k];
				}
			}
		}
		return false;
	}
	/*
		优化弹窗任务
	*/
	public function pop_game($plugin_name='',$type=0){
		if($_SESSION['uid']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0);
			if($plugin_name){
				$params['plugin_name']=$plugin_name;
			}
			if($type){
				$params['type']=$type;
			}
			$game=$this->pdo_get('game_wait',$params);
			if($game['id']){
				$gameinfo=$this->pdo_get('game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$game['game_id'],'status'=>1));
				if($gameinfo){
					$game=array_merge($gameinfo,$game);
					$game['game_id']=$gameinfo['game_id'];
					$nickname=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'nickname');
					$nickname=$nickname?cfc('tools')->substr($nickname,0,12).'**':'匿名用户';
					$game['sendtitle']=str_replace('[nickname]',$nickname,$game['sendtitle']);
					$game['senddesc']=str_replace('[nickname]',$nickname,$game['senddesc']);
					if($game['action']){
					  $game['url']=$game['action'].'/id='.$gameinfo['id'];
					}elseif($game['plugin_name'] && $game['plugin_name']!='jy_h5game' && $game['plugin_name']!='core'){
						$game['url']=$game['plugin_name'].'/index//id='.$gameinfo['id'];
					}
					return $game;
				}else{
					$this->pdo_update('game_wait',array('status'=>4),array('uniacid'=>$_SESSION['uniacid'],'id'=>$game['id']));	
					$this->pop_game($plugin_name,$type);
					return false;
				}
			}
		}
		return false;
	}
	/*
		自动评价
	*/
	public function int_comments(){
		$shopset=$this->plugin_setting('shopset');
		if($shopset['comment_task']){
			$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3,'iscomment'=>0);
			$shopset['comment_days']=$shopset['comment_days']?:1;
			$params['finishtime <']=TIMESTAMP-$shopset['comment_days']*24*3600;
			$list = pdo_getall('jy_weishop_order',$params,array('id','mid','finishtime','createtime'));
			pdo_delete('jy_weishop_goods_comments',array('createtime <'=>500000,'uniacid'=>$_SESSION['uniacid']));
			if($list){
				foreach($list as $order){
					$comments = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>intval($order['id'])));
					if(!$comments){
						$goods = pdo_getall('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']),array('id','mid','goodsid'));
						foreach($goods as $g){
							$order['finishtime']=$order['finishtime']?:$order['createtime']+2*24*3600;
							$createtime=$order['finishtime']+24*3600;
							$insert=array(
								'uniacid'=>$_SESSION['uniacid'],
								'mid'=>$order['mid'],
								'show'=>1,
								'content'=>$shopset['comment_content']?:'该顾客感觉不错，给出5星好评!',
								'star'=>5,
								'goodsid'=>intval($g['goodsid']),
								'orderid'=>$order['id'],
								'createtime'=>$createtime,
							);
							$this->pdo_insert('goods_comments',$insert);
						}
					}
					$this->update('order',array('iscomment'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
					if($order['merchid'] && $insert){
						model('jy_merch/model')->upcomments($insert,$order['merchid']);
					}
				}
			}
			return false;
		}
	}
	/*
		执行脚本
	*/
	public function update_sql(){
		if(!$_SESSION['isfounder']){
			return false;
		}
		$updatelog=core_setting('updatelog');
		if($updatelog['jy_weishop']>=7){
			return false;
		}
		if(pdo_fieldexists('jy_weishop_goods','commission1_rate')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN commission1_rate;');
		}
		if(pdo_fieldexists('jy_weishop_goods','commission1_pay')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN commission1_pay;');
		}
		if(pdo_fieldexists('jy_weishop_goods','commission2_rate')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN commission2_rate;');
		}
		if(pdo_fieldexists('jy_weishop_goods','commission2_pay')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN commission2_pay;');
		}
		if(pdo_fieldexists('jy_weishop_goods','commission3_rate')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN commission3_rate;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_logo')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_logo;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_title')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_title;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_btntext1')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_btntext1;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_btnurl1')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_btnurl1;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_btntext2')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_btntext2;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_btnurl2')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_btnurl2;');
		}
		if(pdo_fieldexists('jy_weishop_goods','store_desc')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN store_desc;');
		}
		if(pdo_fieldexists('jy_weishop_goods','buyagain')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN buyagain;');
		}
		if(pdo_fieldexists('jy_weishop_goods','buyagain_islong')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN buyagain_islong;');
		}
		if(pdo_fieldexists('jy_weishop_goods','buyagain_condition')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN buyagain_condition;');
		}
		if(pdo_fieldexists('jy_weishop_goods','buyagain_issale')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN buyagain_issale;');
		}
		if(pdo_fieldexists('jy_weishop_goods','buyagain_iscommission')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN buyagain_iscommission;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellprice')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellprice;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellover')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellover;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellovertime')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellovertime;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellonlinetime')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellonlinetime;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellsendtimetype')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellsendtimetype;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellsendstatrttime')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellsendstatrttime;');
		}
		if(pdo_fieldexists('jy_weishop_goods','presellsendtime')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN presellsendtime;');
		}
		if(pdo_fieldexists('jy_weishop_goods','fullareas_code')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN fullareas_code;');
		}
		if(pdo_fieldexists('jy_weishop_goods','exchange_stock')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN exchange_stock;');
		}
		if(pdo_fieldexists('jy_weishop_goods','exchange_postage')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN exchange_postage;');
		}
		if(pdo_fieldexists('jy_weishop_goods','merch_paixu')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN merch_paixu;');
		}
		if(pdo_fieldexists('jy_weishop_goods','unite_total')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN unite_total;');
		}
		if(pdo_fieldexists('jy_weishop_goods','threen')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN threen;');
		}
		if(pdo_fieldexists('jy_weishop_goods','intervalfloor')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN intervalfloor;');
		}
		if(pdo_fieldexists('jy_weishop_goods','intervalprice')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN intervalprice;');
		}
		if(pdo_fieldexists('jy_weishop_goods','options')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN options;');
		}
		if(pdo_fieldexists('jy_weishop_goods','officimg')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_goods DROP COLUMN officimg;');
		}
		if(pdo_fieldexists('jy_weishop_order','merchshow')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_order DROP COLUMN merchshow;');
		}
		if(pdo_fieldexists('jy_weishop_order','merchdeductnum')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_order DROP COLUMN merchdeductnum;');
		}
		if(pdo_fieldexists('jy_weishop_order','merchisdiscountprice')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_order DROP COLUMN merchisdiscountprice;');
		}
		if(pdo_fieldexists('jy_weishop_order','coupongoodprice')){
			pdo_query('ALTER TABLE '.TABLE_PRE.'jy_weishop_order DROP COLUMN coupongoodprice;');
		}
		$updatelog['jy_weishop']=8;
		core_setting('updatelog',$updatelog);
		return true;
	}
}